package org.example.javax.security.jaas.demo3;

import java.io.File;
import java.io.IOException;
import java.security.AccessControlException;
import java.security.AccessController;
import java.security.PrivilegedAction;

/**
 * @author JuntaoZhang
 */
public class FileUtil {
  public static void makeFile(String fileName) {
    try {
      // 尝试在工程 A 执行文件的路径中创建一个新文件
      File fs = new File(fileName);
      fs.createNewFile();
    } catch (AccessControlException | IOException e) {
      e.printStackTrace();
    }
  }

  public static void doPrivilegedAction(final String fileName) {
    // 用特权访问方式创建文件
    AccessController.doPrivileged(new PrivilegedAction<String>() {
      @Override
      public String run() {
        makeFile(fileName);
        return null;
      }
    });
  }

  public static void main(String[] args) {
    FileUtil.makeFile("./data/test.txt");
  }
}
